/*-
 * LogStatement.java
 *
 * $LastChangedBy:  $
 * $LastChangedRevision:  $
 * $LastChangedDate:  $
 * $HeadURL: $
 *
 * Copyright (c) 2007, Stein Gunnar Bakkeby
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Stein Gunnar Bakkeby nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.attention.util;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * This class represents a log statement. Whenever instances of this  
 *
 * @author Stein Gunnar Bakkeby
 */
public class LogStatement {
  
  private final String logStatement;
  private final Level  logLevel;
  private final Logger logger;
  
  /**
   * Constructor, creates the log statement with the log string, the log level,
   * and the logger to log to.
   * 
   * @param logStatement The string to log
   * @param logLevel The level to log at
   * @param logger The logger to log to
   */
  public LogStatement(final String logStatement,
                      final Level  logLevel,
                      final Logger logger) {
    this.logStatement = logStatement;
    this.logLevel = logLevel;
    this.logger = logger;
  }
  
  /**
   * Logs the current enum's log statement.
   * 
   * @return Log statement
   */
  @Override
  public String toString() {
    return logStatement;
  }
  
  /**
   * Log with 0 or more string parameters.
   * 
   * @param values See the parameters for the enum that will log this message.
   */
  public void log(final Object ... values) {
    if (logger.isEnabledFor(logLevel)) {
      logger.log(logLevel, parse(values));
    }
  }

  /**
   * Log an exception with the current enum.
   * 
   * @param e The exception to log.
   */
  public void log(final Exception e) {
    if (logger.isEnabledFor(logLevel)) {
      logger.log(logLevel, "", e);
    }
  }
  
  /**
   * Logs an exception with 0 or more string parameters.
   * 
   * @param e The exception to log.
   * @param values See the parameters for the enum that will log this message.
   */
  public void log(final Exception e, final Object ... values) {
    if (logger.isEnabledFor(logLevel)) {
      logger.log(logLevel, parse(values), e);
    }
  }
  
  private String parse(final Object ... values) {
    final int size = values.length - 1;
    String returnString = logStatement;
    for (int i = size; i >= 0; i--) {
      returnString = returnString.replace("%" + i, values[i].toString());
    }
    return returnString;
  }
}